#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

// 存钱
void Deposit(mutex &m, int &money) {
    for (int i = 0; i < 100; i++) {
        m.lock();
        money += 1;
        m.unlock();
    }
}

// 取钱
void Withdraw(mutex &m, int &money) {
    for (int i = 0; i < 200; i++) {
        m.lock();
        money -= 1;
        m.unlock();
    }
}

int main() {
    int money = 2000;
    mutex g_m;
    cout << "start money: " << money << endl;
    thread t1(Deposit, ref(g_m), ref(money));
    thread t2(Withdraw, ref(g_m), ref(money));

    t1.join();
    t2.join();

    cout << "finish money: " << money << endl;

    return 0;
}
